"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebReqHandler = void 0;
const Logger_1 = require("../utils/Logger");
const RetCodeDefine_1 = require("../../config/RetCodeDefine");
const RedisManager_1 = require("../utils/RedisManager");
const RedisConfig_1 = require("../../config/RedisConfig");
const ZoneModel_1 = require("../../model/ZoneModel");
const UserModel_1 = require("../../model/UserModel");
const IpUtil_1 = require("../utils/IpUtil");
class WebReqHandler {
    init(app) {
        // app.use("/serverlist", this.onGetServerList)
        app.use("/", this.onGetH5);
        app.use("/h2", this.onGetH5);
    }
    checkActiveIp(ip) {
        //  17.0.0.0 ~ 17.255.255.255
        // 63.92.224.0 ~ 63.92.255.255
        // 65.199.22.0 ~ 65.199.23.255
        // 192.12.74.0 ~ 192.12.74.255
        // 192.42.249.0 ~ 192.42.249.255
        // 204.79.190.0 ~ 204.79.190.255
        // 144.178.0.0 ~ 144.178.63.255
        let ipArr = ip.split(".");
        if (ipArr.length >= 4) {
            if (ipArr[0] == "17") {
                return false;
            }
            else if (ipArr[0] == "63") {
                return false;
            }
            else if (ipArr[0] == "65") {
                return false;
            }
            else if (ipArr[0] == "192" && ipArr[1] == "42" && ipArr[2] == "249") {
                return false;
            }
            else if (ipArr[0] == "204" && ipArr[1] == "79" && ipArr[2] == "190") {
                return false;
            }
            else if (ipArr[0] == "144" && ipArr[1] == "178") {
                return false;
            }
            else {
                if (IpUtil_1.IpUtil.isInChina(ip)) {
                    return true;
                }
                return false;
            }
        }
        else {
            return false;
        }
    }
    onGetH5(req, resp) {
        return __awaiter(this, void 0, void 0, function* () {
            let ip = req.headers['X-Real-IP'] || req.connection.remoteAddress;
            if (ip.indexOf('::ffff:') != -1) {
                ip = ip.substring(7);
            }
            Logger_1.Logger.log("req==", req);
            resp.setHeader("Access-Control-Allow-Origin", "*");
            let isIpActive = WebReqHandler.instance.checkActiveIp(ip);
            Logger_1.Logger.log("ip==", ip, isIpActive);
            if (req.originalUrl == "/h2") {
                resp.redirect(302, 'http://172.81.214.222:9601');
            }
            else {
                if (isIpActive) {
                    resp.redirect(302, 'http://oauth.aiyinghun.com/fusion/app/?gid=120000123&cid=2&pkg=bnweb3&appid=110001188');
                }
                else {
                    resp.redirect(302, 'http://212.64.108.20:9606/');
                }
            }
        });
    }
    onGetServerList(req, resp) {
        return __awaiter(this, void 0, void 0, function* () {
            Logger_1.Logger.log("onGetServerList==query", req.query);
            let ret = RetCodeDefine_1.RetCodeDefine.OK;
            let result = {};
            if (req.query && req.query["userName"]) {
                let redis = RedisManager_1.RedisManager.instance.getRedis(RedisConfig_1.RedisConfig.Database_Index.Users);
                let data = yield redis.getValuePromise(ZoneModel_1.ZoneModel.Redis_Key);
                result["serverList"] = data;
                let userName = req.query["userName"];
                let myData = yield UserModel_1.UserModel.getUserFromRedis(userName);
                Logger_1.Logger.log("userdatas==", myData);
                if (myData) {
                    let obj = JSON.parse(myData);
                    result["myServerList"] = obj["serverIdList"];
                }
                else {
                    Logger_1.Logger.log("不存在该用户信息", userName);
                }
            }
            else {
                ret = RetCodeDefine_1.RetCodeDefine.Error_Param;
            }
            result["ret"] = ret;
            result["msg"] = RetCodeDefine_1.RetCodeDefine.code2Msg.get(ret);
            let resultStr = JSON.stringify(result);
            Logger_1.Logger.log("result==", resultStr);
            resp.end(resultStr);
        });
    }
}
exports.WebReqHandler = WebReqHandler;
WebReqHandler.instance = new WebReqHandler();
